package com.avic.rong.search.fragment.searchResultFragments;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.MessageEvent;
import com.avic.rong.search.activity.SearchResaultActivity;
import com.avic.rong.search.adapter.searchResultFragmentsAdapters.SearchVpPeiXunRvAdapter;
import com.avic.rong.search.api.secondViewApi.SearchHomeQuanBuApi;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchPeiXunFragment extends BaseFragment {

    @BindView(R.id.search_PeiXun_Rv)
    RecyclerView searchPeiXunRv;
    Unbinder unbinder;
    @BindView(R.id.search_vp_peixun_tv)
    TextView searchVpPeixunTv;
    @BindView(R.id.beijing_image)
    ImageView beijing_image;
    @BindView(R.id.search_fragment_peixun_SmartRefreshLayout)
    SmartRefreshLayout searchFragmentPeixunSmartRefreshLayout;
    private View view;
    private List<GoodsSortSearchAllBzBean.DataBean> list = new ArrayList<>();
    private SearchVpPeiXunRvAdapter adapter;

    private String currentText = "GJB";
    private static int pageNo = 1;

    public SearchPeiXunFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        currentText = SearchResaultActivity.nowSearchStr;
        Log.e("cjx", "SearchQuanBuFragment--------currentText:" + currentText);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_pei_xun, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        list.clear();
        pageNo = 1;
        initPeiXunData(currentText, pageNo);
        searchPeiXunRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SearchVpPeiXunRvAdapter(getContext(), list);
        searchPeiXunRv.setAdapter(adapter);
        setRefresh();
        return view;
    }

    private void setRefresh() {
        searchFragmentPeixunSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                pageNo = 1;
                initPeiXunData(currentText, pageNo);
                searchFragmentPeixunSmartRefreshLayout.finishRefresh(1000);
            }
        });
        searchFragmentPeixunSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                ++pageNo;
                initPeiXunData(currentText, pageNo);
                searchFragmentPeixunSmartRefreshLayout.finishLoadMore(1000);
            }
        });
    }

    private void initPeiXunData(String keyword, int pageNo) {
        SearchHomeQuanBuApi searchAllApi = new SearchHomeQuanBuApi((BaseActivity) getActivity(), searchHomeQuanBuListener, "quanbu", 4, keyword, pageNo, 10);
        searchAllApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
    }

    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> searchHomeQuanBuListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {

                return;
            }

            ArrayList<GoodsSortSearchAllBzBean> searchHomeQuanBuList = (ArrayList<GoodsSortSearchAllBzBean>) subjects.getData();
            list.addAll(searchHomeQuanBuList.get(2).getData());

            adapter.notifyDataSetChanged();

            if (list.size() > 0) {
                searchPeiXunRv.setVisibility(View.VISIBLE);
                searchVpPeixunTv.setVisibility(View.GONE);
                beijing_image.setVisibility(View.GONE);

            } else {
                searchPeiXunRv.setVisibility(View.GONE);
                searchVpPeixunTv.setVisibility(View.VISIBLE);
                beijing_image.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };


    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void cha(MessageEvent msg) {
        if (msg != null) {
            if (msg.getMessage().equals("2")) {
                pageNo = 1;
                initPeiXunData(currentText, pageNo);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }
}
